<script setup lang="ts">
import { reactive, ref, computed, onMounted, onUnmounted } from 'vue'
import { getSectorFunds } from '../api/api'
import { getTime1 } from '../utils/timeUtil'

const showFirstTable = ref(true)
const autoPlayID = ref(-1)

const tableData1 = reactive([] as { changeDate: string; plateName: string }[])
const tableData2 = reactive([] as { changeDate: string; plateName: string }[])

const tableData = computed(() =>
  showFirstTable.value ? tableData1 : tableData2
)

const renderChangeDate = (row: { changeDate: number | string }) => {
  let changeValue: number

  if (typeof row.changeDate === 'number') {
    changeValue = row.changeDate
  } else if (typeof row.changeDate === 'string') {
    changeValue = parseFloat(row.changeDate.replace('%', ''))
  } else {
    return `<span>Invalid Data</span>`
  }

  const isPositive = changeValue > 0
  const color = isPositive ? 'red' : 'green'
  const arrow = isPositive ? '⬆' : '⬇'

  return `<span style="color: ${color}">${changeValue}% ${arrow}</span>`
}

const toggleTable = () => {
  showFirstTable.value = !showFirstTable.value
}

const fetchTableData = async () => {
  try {
    const response = await getSectorFunds()

    if (response && response.data && response.data.status === 'success') {
      const data = response.data.data
      data.top_gainers = getTime1(data.top_gainers)
      data.top_losers = getTime1(data.top_losers)
      data.top_gainers = data.top_gainers.filter(
        (item) => item.time === data.top_gainers[0].time
      )
      data.top_losers = data.top_losers.filter(
        (item) => item.time === data.top_losers[0].time
      )
      //根据change排序
      data.top_losers.sort((a, b) => a.change - b.change)
      data.top_gainers.sort((a, b) => b.change - a.change)
      const parsedTableData1 = data.top_gainers.map((item) => ({
        plateName: item.sector_name,
        changeDate: item.change,
      }))

      const parsedTableData2 = data.top_losers.map((item) => ({
        plateName: item.sector_name,
        changeDate: item.change,
      }))

      tableData1.splice(0, tableData1.length, ...parsedTableData1)
      tableData2.splice(0, tableData2.length, ...parsedTableData2)
    } else {
      console.error('Error: API response was not successful.')
    }
  } catch (error) {
    console.error('Error fetching table data:', error)
  }
}
//自动更新事件（十分钟）
const startDIAutoPlay = () => {
  autoPlayID.value = setInterval(fetchTableData, 1000 * 60 * 10)
}
const stopDIAutoPlay = () => {
  clearInterval(autoPlayID.value)
}

onMounted(() => {
  fetchTableData()
  startDIAutoPlay()
})
onUnmounted(() => {
  stopDIAutoPlay()
})
</script>

<template>
  <div class="card">
    <div class="china_index_content">
      <div class="china_index_title">
        <span>板块资金流今日跌涨幅</span>
        <!-- <button  class="toggle-button">
          <span v-if="showFirstTable">切换当日跌幅</span>
          <span v-else>切换当日涨幅</span>
        </button> -->
      </div>
      <div class="china_index_table">
        <div class="china_index_tablehead">
          <span>板块名称</span>
          <span
            style="cursor: pointer; user-select: none"
            @click="toggleTable"
            >{{ showFirstTable ? '当日涨幅' : '当日跌幅' }}</span
          >
        </div>
        <div id="china_index_data_container">
          <div class="china_index_carousel_content_framework">
            <div class="china_index_data_content">
              <div
                class="china_index_data_column"
                v-for="row in tableData"
                :key="row.plateName"
              >
                <span>{{ row.plateName }}</span>
                <span v-html="renderChangeDate(row)"></span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="china_index_background">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
    </div>
  </div>
</template>

<style scoped>
.card {
  width: 37%;
  height: 47%;
  margin: 1%;
}
.china_index_content {
  padding: 2px;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  grid-row: 1;
  grid-column: 1;
  z-index: 1;
}

.china_index_title {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background: linear-gradient(to right, #05174d 0%, #043b9a 50%, #05174d 100%);
  box-sizing: border-box;
}

.china_index_title span {
  font-size: 20px;
  font-weight: bolder;
  letter-spacing: 2px;
  color: white;
}

.toggle-button {
  cursor: pointer;
  background-color: #20489b;
  border: none;
  border-radius: 20px;
  padding: 5px 10px;
  margin-left: 10px;
}

.toggle-button span {
  font-size: 16px;
  color: white;
}

.china_index_table {
  width: 100%;
  height: 100%;
  padding: 10px;
}

.china_index_tablehead {
  display: flex;
  width: 100%;
  height: 34px;
  line-height: 34px;
  background-color: #06245e;
  color: #bdbfc7;
  font-weight: bolder;
  font-family: '黑体';
  font-size: 17px;
}

.china_index_tablehead > span {
  display: inline-block;
}

.china_index_tablehead > :nth-child(1) {
  width: 65%;
  padding-left: 20px;
}

.china_index_tablehead > :nth-child(2) {
  width: 35%;
  padding-left: 10px;
}

#china_index_data_container {
  position: relative;
  width: 100%;
  height: calc(100% - 86px);
  margin-top: 10px;
  background-color: #06245e;
  padding-top: 1%;
  overflow: auto;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.china_index_carousel_content_framework {
  width: 100%;
  height: 100%;
  position: relative;
}

.china_index_data_content {
  width: 100%;
  height: 100%;
  overflow-y: scroll;
}

.china_index_data_column {
  display: flex;
  justify-content: space-between;
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  color: #d0d0d0;
  font-family: 'Microsoft YaHei', Arial, sans-serif;
  font-size: 18px;
}

.china_index_data_column > span:first-child {
  width: 65%;
  padding-left: 20px;
  font-stretch: extra-expanded;
}

.china_index_data_column > span:last-child {
  width: 35%;
  padding-left: 10px;
  color: #30c2ff;
}

.china_index_background {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #05174d;
  border: #20489b 1px solid;
  grid-row: 1;
  grid-column: 1;
  z-index: 0;
  overflow: visible;
}

.china_index_background > span {
  position: absolute;
  width: 20px;
  height: 20px;
}

.china_index_background > span:nth-child(1) {
  top: -2px;
  left: -2px;
  border-left: 3px solid #20489b;
  border-top: 3px solid #20489b;
}

.china_index_background > span:nth-child(2) {
  top: -2px;
  right: -2px;
  border-top: 3px solid #20489b;
  border-right: 3px solid #20489b;
}

.china_index_background > span:nth-child(3) {
  bottom: -2px;
  left: -2px;
  border-left: 3px solid #20489b;
  border-bottom: 3px solid #20489b;
}

.china_index_background > span:nth-child(4) {
  bottom: -2px;
  right: -2px;
  border-bottom: 3px solid #20489b;
  border-right: 3px solid #20489b;
}
</style>
